import { useEffect } from "./useEffect";

export function useDocumentEvent<K extends keyof DocumentEventMap>(event: K, listener: (ev: DocumentEventMap[K]) => void) {
  useEffect(() => {
    document.addEventListener(event, listener);
    return () => document.removeEventListener(event, listener);
  });
}

export function useDomEvent<K extends keyof ElementEventMap>(
  el: HTMLElement,
  event: K,
  listener: (ev: ElementEventMap[K]) => void,
) {
  useEffect(() => {
    el.addEventListener(event, listener);
    return () => el.removeEventListener(event, listener);
  });
}
